package org.zjut.db;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zjut.util.CommonUtil;

import java.util.*;

/**
 * @author Rising
 * @date 2017/8/28
 * @des 学习概念增删查检（知识点）
 */
public class LearnConceptDataBase extends JdbcDaoSupport {
    String dataBase = "pso_recommond";
    String tb_knowledge = ".knowledge";


    /**
     * 新增学习者概念
     * @param params
     * @return
     */
    public Map<String,Object > addLeanConcept(Map<String,Object> params){
        String conceptName = null,courseCode = null, describe = null,userCode=null;
        Map<String,Object> result = new HashMap<>();
        try{
            conceptName = params.get("conceptName").toString();
            courseCode = params.get("courseCode").toString();
            describe = params.get("describes").toString();
            userCode = params.get("userCode").toString();
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "INSERT INTO "+dataBase+tb_knowledge+" (code,courseCode,name,_describe,userCode)VALUES(?,?,?,?,?) ";
        Map<String,Object> message = new HashMap<>();
        try{
            String code = UUID.randomUUID().toString();
            int row = getJdbcTemplate().update(sql,new Object[]{code,courseCode,conceptName,describe,userCode});
            if(row > 0){
                message.put("conceptCode",code);
                message.put("courseCode",courseCode);
                message.put("describes",describe);
                message.put("conceptName",conceptName);
                message.put("userCode",userCode);
            }else{
                return CommonUtil.setErrorInfo("新增失败");
            }
        }catch (Exception e){
            throw e;
        }
        result.put("success",true);
        result.put("message",message);
        return result;
    }


    /**
     * 获取学习者概念
     * @param params
     * @return
     */
    public Map<String,Object> getLeanConcept(Map<String,Object> params){
        System.out.println("xxxxxxxxx");
        String conceptName = null, conceptCode = null, courseCode = null , userCode = null;
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> message = new HashMap<>();
        List<Map<String,Object>> leanConcept = new ArrayList<>();
        if(params!=null){

            if(params.containsKey("conceptName")){
                conceptName = params.get("conceptName").toString();
            }
            if(params.containsKey("courseCode")){
                courseCode = params.get("courseCode").toString();
            }
            if(params.containsKey("conceptCode")){
                conceptCode = params.get("conceptCode").toString();
            }
            if(params.containsKey("userCode")){
                userCode = params.get("userCode").toString();
            }
        }

        String sql = "SELECT a.code as conceptCode, a.name as conceptName,a._describe as describes FROM "+dataBase+tb_knowledge+ " as a  WHERE a.isDelete =0  ";
        if(conceptName != null){
            sql = sql+ " AND a.name like '%"+conceptName+"%'  ";
        }
        if(conceptCode != null){
            sql = sql+ " AND a.code ='"+conceptCode+"'";
        }

        if(courseCode!=null){
            sql = sql + " AND a.courseCode = '"+courseCode+"'";
        }
        if(userCode!=null){
            sql = sql + " AND a.userCode = '"+userCode+"'";
        }

        System.out.println(sql);
        try{
            leanConcept = getJdbcTemplate().queryForList(sql);
            message.put("conceptInfo",leanConcept);
        }catch (Exception e){
            System.out.println("getLeanConcept"+e.getMessage());
            return CommonUtil.setErrorInfo("查询失败");
        }
        result.put("success",true);
        result.put("message",message);
        return result;
    }

    /**
     * 删除学习概念
     * @param params
     * @return
     */
    @Transactional(propagation= Propagation.REQUIRED)
    public Map<String,Object> delLearnConcept(Map<String,Object> params){
        Map<String,Object> result = new HashMap<>();
        String conceptCode = null;
        try{
            conceptCode = params.get("conceptCode").toString();
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "UPDATE "+dataBase+tb_knowledge+" SET isDelete = 1 WHERE code = ? ";

        try{
            int row = getJdbcTemplate().update(sql,new Object[]{conceptCode});
            if(row <= 0){
                return CommonUtil.setErrorInfo("删除失败");
            }
        }catch (Exception e){
            throw e;
        }

        result.put("success",true);
        result.put("message","删除成功");
        return result;
    }


    /**
     * 修改学习概念
     * @param params
     * @return
     */
    public Map<String,Object> editLearnConcept(Map<String,Object> params){
        Map<String,Object> result = new HashMap<>();
        String conceptCode = null , conceptName = null, describe = null;

        try{
            conceptCode = params.get("conceptCode").toString();
            conceptName = params.get("conceptName").toString();
            describe = params.get("describes").toString() ;
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "UPDATE "+dataBase+tb_knowledge+" SET name = ?,_describe = ?  WHERE code = ? ";

        try{
            getJdbcTemplate().update(sql,new Object[]{conceptName,describe,conceptCode});
        }catch (Exception e){
            throw e;
        }

        result.put("success",true);
        result.put("message","修改成功");

        return result;
    }


    /**
     * 恢复删除的某个知识点
     * @param params
     * @return
     */
    public Map<String,Object> rowBackTheDelConcept(Map<String,Object> params){
        String conceptCode = null;
        Map<String,Object> result = new HashMap<>();

        try {
            conceptCode = params.get("conceptCode").toString();
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "UPDATE "+dataBase+".lean_concept SET isDelete = ?  WHERE code = ? ";

        try{
            //假删除
            int row  = getJdbcTemplate().update(sql,new Object[]{0,conceptCode});
            if(row > 0 ){
                result.put("success",true);
                result.put("message","修改成功");
            }else{
                return CommonUtil.setErrorInfo("数据恢复失败");
            }
        }catch (Exception e){
            throw e;
        }

        result.put("success",true);
        result.put("message","修改成功");

        return result;
    }
}
